package com.hh.game.service.gateway.webscoket.handler;

import com.hh.game.common.base.manager.session.GameSession;
import com.hh.game.common.base.manager.session.Session;
import com.hh.game.service.gateway.codec.websocket.WebSocketRequest;
import com.hh.game.service.gateway.pool.GatewayWorkPool;
import com.hh.game.service.gateway.codec.websocket.InputMessage;
import com.hh.game.service.gateway.webscoket.config.WebSocketServerConfig;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GatewayHandler extends SimpleChannelInboundHandler<WebSocketRequest> {
    private Logger log = LoggerFactory.getLogger(GatewayHandler.class);

//    private static ExecutorService executorService;
    private WebSocketServerConfig config;
    private Session session;


    public GatewayHandler(WebSocketServerConfig webSocketServerConfig) {
        this.config = webSocketServerConfig;
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        Channel channel = ctx.channel();
        GameSession session = new GameSession(channel.id().toString(), channel);
        ctx.channel().attr(Session.SESSION_KEY).set(session);
        this.session = session;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, WebSocketRequest request) throws Exception {
        GatewayWorkPool.addWork(request);
    }

}
